
--[[
Exmple:

CustomEvents('event_name', function(e, data)
	local player = PlayerResource:GetPlayer(data.PlayerID)
	if player ==nil then return end

	local hero = player:GetAssignedHero()
	if hero == nil then return end
end)

]]

------------------------------------------------------------

CustomEvents('avalon_get_item_tooltip_data', function(e, data)
	local player = PlayerResource:GetPlayer(data.PlayerID)
	if player ==nil then return end

	local hero = player:GetAssignedHero()
	if hero ==nil then return end

	-- 获取物品属性
	local item = EntIndexToHScript(data.item)
	if not item then return end

	local custom_attributes
	if item:HasCustomAttributes() then
		custom_attributes = item:GetAllCustomAttribute()
	end

	local compare = true
	for i=0,5 do
		local _item = hero:GetItemInSlot(i)
		if _item == item then
			compare = false
			break
		end
	end

	local compareItem1
	local compareItem1_Config
	local compareItem1_Specials
	local compareItem1_CustomAttributes
	local compareItem1_CustomData
	local compareItem2
	local compareItem2_Config
	local compareItem2_Specials
	local compareItem2_CustomAttributes
	local compareItem2_CustomData
	
	-- if compare then
	-- 	-- 获取比较的装备
	-- 	local inventorySlot = EquipCtrl:GetSlot(item)
	-- 	if inventorySlot >= 0 then
	-- 		local inventoryItem = hero:GetItemInSlot(inventorySlot)
	-- 		if inventoryItem and inventoryItem ~= item then

	-- 			compareItem1 = inventoryItem:GetEntityIndex()
	-- 			compareItem1_Config = ItemConfig[inventoryItem:GetAbilityName()]
	-- 			compareItem1_Specials = inventoryItem:GetAbilityKeyValues()["AbilitySpecial"]
	-- 			compareItem1_CustomData = inventoryItem:GetCustomData()
	-- 			if inventoryItem:HasCustomAttributes() then
	-- 				compareItem1_CustomAttributes = inventoryItem:GetAllCustomAttribute()
	-- 			end

	-- 		end

	-- 		-- 如果是饰品湖区另外一个格子的饰品
	-- 		if inventorySlot == ITEM_KIND_GROUP_TRINKET_SLOT then
	-- 			local inventoryItem2 = hero:GetItemInSlot(inventorySlot+1)
	-- 			if inventoryItem2 and inventoryItem2 ~= item then
	-- 				compareItem2 = inventoryItem2:GetEntityIndex()
	-- 				compareItem2_Config = ItemConfig[inventoryItem2:GetAbilityName()]
	-- 				compareItem2_Specials = inventoryItem2:GetAbilityKeyValues()["AbilitySpecial"]
	-- 				compareItem2_CustomData = inventoryItem2:GetCustomData()
	-- 				if inventoryItem2:HasCustomAttributes() then
	-- 					compareItem2_CustomAttributes = inventoryItem2:GetAllCustomAttribute()
	-- 				end
	-- 			end
	-- 		end
	-- 	end
	-- end

	CustomGameEventManager:Send_ServerToPlayer(player, "avalon_get_item_tooltip_data_response", {
		item=data.item, specials=item:GetAbilityKeyValues()["AbilitySpecial"], custom_attributes=custom_attributes, config=ItemConfig[item:GetAbilityName()],
		custom_data = item:GetCustomData(),

		compareItem1=compareItem1, compareItem1_Specials=compareItem1_Specials,compareItem1_CustomAttributes=compareItem1_CustomAttributes, 
		compareItem1_Config=compareItem1_Config, compareItem1_CustomData=compareItem1_CustomData,

		compareItem2=compareItem2, compareItem2_Specials=compareItem2_Specials,compareItem2_CustomAttributes=compareItem2_CustomAttributes,
		compareItem2_Config=compareItem2_Config, compareItem2_CustomData=compareItem2_CustomData,
		})
end)

-------------------------------------------------------------------------

CustomEvents('avalon_get_item_tooltip_data_for_kv', function(e, data)
	print("avalon_get_item_tooltip_data_for_kv")
	for k, v in pairs(data) do
		print(k..v)
	end
	local player = PlayerResource:GetPlayer(data.PlayerID)
	if player ==nil then return end

	local hero = player:GetAssignedHero()
	if hero ==nil then return end

	local itemname = data.itemname or ""
	local kv = ItemsKV[itemname]
	if not kv then return end

	local compareItem1
	local compareItem1_Config
	local compareItem1_Specials
	local compareItem1_CustomAttributes
	local compareItem1_CustomData
	local compareItem2
	local compareItem2_Config
	local compareItem2_Specials
	local compareItem2_CustomAttributes
	local compareItem2_CustomData
	
	-- -- 获取比较的装备
	-- local inventorySlot = EquipCtrl:GetSlotForItemName(itemname)
	-- if inventorySlot >= 0 then
	-- 	local inventoryItem = hero:GetItemInSlot(inventorySlot)
	-- 	if inventoryItem then

	-- 		compareItem1 = inventoryItem:GetEntityIndex()
	-- 		compareItem1_Config = ItemConfig[inventoryItem:GetAbilityName()]
	-- 		compareItem1_Specials = inventoryItem:GetAbilityKeyValues()["AbilitySpecial"]
	-- 		compareItem1_CustomData = inventoryItem:GetCustomData()
	-- 		if inventoryItem:HasCustomAttributes() then
	-- 			compareItem1_CustomAttributes = inventoryItem:GetAllCustomAttribute()
	-- 		end

	-- 	end

	-- 	-- 如果是饰品湖区另外一个格子的饰品
	-- 	if inventorySlot == ITEM_KIND_GROUP_TRINKET_SLOT then
	-- 		local inventoryItem2 = hero:GetItemInSlot(inventorySlot+1)
	-- 		if inventoryItem2 then
	-- 			compareItem2 = inventoryItem2:GetEntityIndex()
	-- 			compareItem2_Config = ItemConfig[inventoryItem2:GetAbilityName()]
	-- 			compareItem2_Specials = inventoryItem2:GetAbilityKeyValues()["AbilitySpecial"]
	-- 			compareItem2_CustomData = inventoryItem2:GetCustomData()
	-- 			if inventoryItem2:HasCustomAttributes() then
	-- 				compareItem2_CustomAttributes = inventoryItem2:GetAllCustomAttribute()
	-- 			end
	-- 		end
	-- 	end
	-- end

	CustomGameEventManager:Send_ServerToPlayer(player, "avalon_get_item_tooltip_data_for_kv_response", {
		itemname = data.itemname, kv=kv, config=ItemConfig[itemname],

		compareItem1=compareItem1, compareItem1_Specials=compareItem1_Specials,compareItem1_CustomAttributes=compareItem1_CustomAttributes, 
		compareItem1_Config=compareItem1_Config, compareItem1_CustomData=compareItem1_CustomData,

		compareItem2=compareItem2, compareItem2_Specials=compareItem2_Specials,compareItem2_CustomAttributes=compareItem2_CustomAttributes,
		compareItem2_Config=compareItem2_Config, compareItem2_CustomData=compareItem2_CustomData,
	})
end)

-------------------------------------------------------------------------
CustomEvents('shushan_sort_publish_stash', function(e, data)
	local player = PlayerResource:GetPlayer(data.PlayerID)
	if player ==nil then return end
	PublicStash:sort()
end)